package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
   private StudentService studentService = new StudentService();
    Scanner sc = new Scanner(System.in);

    //初始化菜单
    public void start() {
        lo:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    // System.out.println("删除学生");
                    deleteStudent();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    changeStuedent();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    searchStudent();
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break lo;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
    }

    //查找
    public void searchStudent() {
        Student[] stu = studentService.useStudent();//判断数组是否为空
        if (stu == null) {
            System.out.println("暂无信息，请先添加");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        studentService.searchStudent();
    }

    //修改
    public void changeStuedent() {
        Student[] stu = studentService.useStudent();//判断数组是否为空
        if (stu == null) {
            System.out.println("暂无信息，请先添加");
            return;
        }
        while (true) {
            System.out.println("请输入要修改的学生id或按N/n键返回上一级菜单");
            String id = sc.next();
            if (id.equalsIgnoreCase("n")) {
                return;
            }
            boolean b = studentService.exit(id);//判断id是否存在
            if (b) {
                Student s = scStudent(id);   //录入基本信息
                studentService.changeStudentById(id, s);
                System.out.println("修改成功");
                break;
            } else {
                System.out.println("查无此人，请重新输入");
            }
        }
    }

    //删除学生对象 通过id
    public void deleteStudent() {
        Student[] stu = studentService.useStudent(); //判断数组是否为空
        if (stu == null) {
            System.out.println("暂无信息，请先添加");
            return;
        }
        while (true) {
            System.out.println("请输入要删除的学生id或按N/n键返回上一级菜单");
            String id = sc.next();
            if (id.equalsIgnoreCase("n")) {
                return;
            }
            boolean b = studentService.exit(id);    //判断id是否存在
            if (b) {
                studentService.deleteStudentById(id);
                System.out.println("删除成功");
                break;
            } else {
                System.out.println("查无此人，请重新输入");
            }
        }

    }

    //添加
    public void addStudent() {
        String id;
        boolean bb = studentService.full();   //判断数组是否已满
        if (bb) {
            System.out.println("用户池已满，请先删除或修改");
            return;
        }
        while (true) {
            System.out.println("请输入学生id或按N/n键返回上一级菜单");
            id = sc.next();
            if (id.equalsIgnoreCase("n")) {
                return;
            }
            boolean b = studentService.exit(id);  //判断id是否存在
            if (b) {
                System.out.println("该学号已被占用，请重新输入");
            } else {
                break;
            }
        }
        Student stu = scStudent(id);  //基本信息录入
        studentService.addStudent(stu);
        System.out.println("添加成功");
    }

    //基本信息录入
    public Student scStudent(String id) {
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        String age = sc.next();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Student stu = new Student(id, name, age, birthday);
        return stu;
    }
}
